//活动轮播
var typeSwiper = new Swiper('.type-swiper', {
	freeMode : true,
	slidesPerView:'6',
	initialSlide :$(".type-swiper").find(".active").parents("li").index(),
})

$(".productList").on("click",".get-coupon",function(e){
	var pass = $(this).attr("data-pass");
	$(".wrapper").find("textarea").text(pass);
	$(".wrapper").find("textarea").val(pass);
	$(".wrapper").fadeIn().css("display","-webkit-box");
})
//禁止编辑
//$(".tpassword").find("textarea").on("input",function(){
//	var text = $(this).text();
//	$(this).text(text);
//})
$(".tpassword").find("textarea").on("input",function(){
	var val = $(this).text();
	$(this).val(val);
})
//禁止那个啥
$(".wrapper-box").click(function(e){
	e.stopPropagation();
})
//关闭窗口
$(".wrapper").click(function(){
    $(this).fadeOut();
})

//切换不用的内部卷分类
$('span[name="internalCertificateCategory"]').click(function(){
	var internalCertificateCategoryName = $(this).attr("data-internalCertificateCategoryName");
	
	if(internalCertificateCategoryName=="quanbu"){
		window.location.href = base_url + "customer/getInternalCertificateMsg";
	}else{
		window.location.href = base_url + "customer/getInternalCertificateMsg?internalCertificateCategoryName="+internalCertificateCategoryName;
	}
	
});

FormatImg($(".p-img"),$(".p-img").parents(".product-img"));

//上拉加载
var buttom = false;
$(document).on("swipeup",function(){
		var winH =$(window).height();
		var myScroll = $(window).scrollTop();
		if(($(document).height()<= winH||($('body').height() - myScroll) <= winH )&&!buttom){
			console.log("到底了");
			buttom=true;
			$(".slideToMore").hide();
			$(".loading").show();
			more();
		}
});

//加载更多
function more(){
	
	var currentPage = $("#currentPage").val();
	var totalPage = $("#totalPage").val();
	var nextPage = 0;
	var internalCertificateCategoryName = $("#internalCertificateCategoryName").val();
	//目前页面的商品数量
	var currentProductNum = $(".productList").find(".product-container").length;
	//回到第一页
	if(parseInt(currentProductNum)<=20){
		currentPage = 1;
	}
	//是否已经到最后 
	if(parseInt(currentPage)+1<=parseInt(totalPage)){
		nextPage = parseInt(currentPage)+1;
		
		if(internalCertificateCategoryName=="quanbu"){
			getInternalCertificateMsg(nextPage,"");
		}else{
			getInternalCertificateMsg(nextPage,internalCertificateCategoryName);
		}
		
	}else{
		$(".slideToMore").text("到底了");
		$(".loading").hide();
		$(".slideToMore").show();
	}
}

function getInternalCertificateMsg(nextPage,internalCertificateCategoryName){
	$(".slideToMore").hide();
	$(".loading").show();
	$.ajax({
	type:"get",
	url:base_url+"customer/getMoreInternalCertificateMsg",
	data:{"internalCertificateCategoryName":internalCertificateCategoryName,"pageNum":nextPage},
	success:function(data){
		$(".loading").hide();
		$(".slideToMore").show();
		var internalCertificateMsgs = data.resultSet;
		for(var i=0;i<internalCertificateMsgs.length;i++){
			var html = 
					'<div class="product-container">'+
						'<div class="product">'+
							'<div class="product-img">'+
								'<img class="p-img lazy" data-src="'+internalCertificateMsgs[i].internalCertificate.surfacePlot+'" data-load="false"/>'+
								'<div class="internal-info">'+
									'<div class="postage">'+
										'<span>包</span>'+
										'<span>邮</span>'+
									'</div>'+
									'<div class="coupon">'+
										'<span>内部券</span>'+
										'<span>'+internalCertificateMsgs[i].internalCertificate.couponQuota+'元</span>'+
									'</div>'+
								'</div>'+
							'</div>'+
							'<p class="product-name text-1">'+
							internalCertificateMsgs[i].internalCertificate.productName+
							'</p>'+
							'<p class="product-price">'+
								'券后'+
								'<span class="price f14">'+internalCertificateMsgs[i].realPrice+'</span>'+
								'<del class="finished">原价：'+internalCertificateMsgs[i].internalCertificate.originalPrice+'元</del>'+
							'</p>'+
							'<p class="get">'+
								'<span>限量'+internalCertificateMsgs[i].internalCertificate.limitation+'张</span>'+
								'<span class="get-coupon" data-pass="复制这条信息，打开【手机淘宝】即可领券下单'+internalCertificateMsgs[i].internalCertificate.taoKouLing+'">领券购买</span>'+
							'</p>'+
						'</div>'+
					'</div>'
			$(".productList").append(html);	
			FormatImg($(".p-img"),$(".p-img").parents(".product-img"));
		}
		buttom = false;
		$("#currentPage").attr("value",data.currentPage);
		$("#totalPage").attr("value",data.totalPage);
	},
	error:function(){
		$(".loading").hide();
		$(".slideToMore").show();
	}

});
}


window.onscroll = function(){
	//懒加载
	lazyLoad($(".lazy[data-load='false']"));
}